package com.lwgk.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.lwgk.AppComplication;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/12
 * --------------------------------------------------------------------------
 * 描述：为判断网络连接的方法，4g，wifi等
 * 注意事项：
 */
public class NetConnectUtils {



    /**
     * 没有连接网络
     */
    private static final int NETWORK_NONE = -1;
    /**
     * 移动网络
     */
    private static final int NETWORK_MOBILE = 0;
    /**
     * 无线网络
     */
    private static final int NETWORK_WIFI = 1;

    /**
     * @return 判断当前是否有网络连接
     */
    public static boolean isNetConnected() {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) AppComplication.mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null&& mNetworkInfo.isConnected()) {
            return mNetworkInfo.isAvailable();
        }
        return false;
    }

    /**
     * @return 判断当前网络是否为wifi
     */
    public static boolean isWifiConnected() {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) AppComplication.mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mWiFiNetworkInfo != null&& mWiFiNetworkInfo.isConnected()) {
                return mWiFiNetworkInfo.isAvailable();
            }
        return false;
    }

    /**
     * @return 判断当前是否为移动网络2g，3g，4g
     */
    public static boolean isMobileConnected() {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) AppComplication.mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mMobileNetworkInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mMobileNetworkInfo != null&& mMobileNetworkInfo.isConnected()) {
                return mMobileNetworkInfo.isAvailable();
            }
        return false;
    }

    /**
     *
     * @return 1,为无线网络，0为移动网络，-1为无网
     */
    public static int getConnectedType() {
        // 得到连接管理器对象
        ConnectivityManager connectivityManager = (ConnectivityManager)  AppComplication.mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                return NETWORK_WIFI;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return NETWORK_MOBILE;
            }
        } else {
            return NETWORK_NONE;
        }
        return NETWORK_NONE;
    }
}
